CFLAGS = -nostdinc -I include -fomit-frame-pointer -O
LDFLAGS = --Ttext=0 --oformat=binary
CDEPEND = -M -I include

KERNEL = kernel/asm.o kernel/traps.o kernel/mktime.o kernel/schedule.o kernel/system_call.o kernel/fork.o kernel/sys.o kernel/keyboard.o kernel/console.o kernel/tty_io.o kernel/printk.o kernel/hd.o kernel/buffer.o kernel/ll_rw_blk.o
MM = mm/memory.o mm/page_fault.o

%.o: %.s
	as $< -o $@

%.o: %.c
	gcc $(CFLAGS) -c $< -o $@

image: boot/bootsect boot/setup system
	dd if=boot/bootsect of=test.img bs=512 count=1 conv=notrunc
	dd if=boot/setup of=test.img seek=1 conv=notrunc
	dd if=system of=test.img seek=5 conv=notrunc
	touch image

boot/bootsect: boot/bootsect.s
	as $< -o $*.o
	ld $(LDFLAGS) -e _start $*.o -o $@

boot/setup: boot/setup.s
	as $< -o $*.o
	ld $(LDFLAGS) -e _setup $*.o -o $@

boot/head.o: boot/head.s

system: boot/head.o main.o temp_kernel temp_mm
	ld $(LDFLAGS) boot/head.o main.o $(KERNEL) $(MM) -o $@

temp_kernel: $(KERNEL)
	(cd kernel; make;)
	touch temp_kernel

temp_mm: $(MM)
	(cd mm; make;)
	touch temp_mm

clean:
	rm -f *.o
	rm -f boot/bootsect boot/setup boot/head.o
	rm -f system image temp_kernel temp_mm
	(cd kernel; make clean)
	(cd mm; make clean)

depend:
	sed '/\#\#\#Dependency/q' < makefile > temp_make
	(for i in *.c; do gcc $(CDEPEND) $$i; done) >> temp_make
	cp temp_make makefile
	rm temp_make
	(cd mm; make depend)
	(cd kernel; make depend)

###Dependency:
main.o: main.c include/system.h include/descriptor.h include/task.h \
 include/sys_call.h include/harddisk.h
